package com.example.excel.ui.adapter;

import android.view.View;

import com.example.excel.R;
import com.example.excel.base.BaseRecyclerViewAdapter;
import com.example.excel.base.CommonViewHolder;
import com.example.excel.bean.InventoryBean;
import com.example.excel.databinding.ItemFormBinding;
import com.example.excel.utils.Utils;

import java.util.List;

public class FormAdapter extends BaseRecyclerViewAdapter<InventoryBean> {
    private final int spanCount;

    public FormAdapter(List<InventoryBean> data, int spanCount) {
        super(data);
        this.spanCount = spanCount;
    }

    public FormAdapter(int spanCount) {
        this.spanCount = spanCount;
    }

    @Override
    protected int layoutId(int viewType) {
        return R.layout.item_form;
    }

    @Override
    protected int variableId() {
        return 0;
    }

    @Override
    public int getItemCount() {
        return getRowCount() * getColumnCount();
    }

    @Override
    public void onBindViewHolder(CommonViewHolder<InventoryBean> holder, int position) {
        int rowIndex = position % getItemCount();
        final int row = rowIndex / getColumnCount();
        int columnIndex = position % getColumnCount();
        ItemFormBinding binding = (ItemFormBinding) holder.getBinding();
        binding.tvItem.setText(FormHelper.getItemData(getData().get(row), columnIndex, row));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mListener != null){
                    mListener.onResult(mData.get(row), row);
                }
            }
        });
    }

    private int getColumnCount() {
        return spanCount;
    }

    private int getRowCount() {
        return getData().size();
    }
}
